﻿@using BlazorApp.Shared
@page "/personedit"
@inject HttpClient Http

<h2>Person Edit</h2>
<p>Id: @person.Id</p>
<p>First name: @person.FirstName</p>
<p>Last name: @person.LastName</p>
<p>Updates: @person.UpdateCounter</p>

<p><button class="btn btn-primary" onclick="@doupdate">Update</button></p>

<p>@Status</p>

@functions {
  BlazorApp.Shared.PersonEdit person = new BlazorApp.Shared.PersonEdit();
  string Status = "Loading";

  protected override async Task OnInitAsync()
  {
    Csla.DataPortalClient.HttpProxy.SetHttpClient(Http);
    try
    {
      person = await Csla.DataPortal.FetchAsync<BlazorApp.Shared.PersonEdit>(123);
    }
    catch (Exception ex)
    {
      Status = ex.ToString();
    }
    Status = "Loaded";
  }

  async Task doupdate()
  {
    Csla.DataPortalClient.HttpProxy.SetHttpClient(Http);
    try
    {
      Status = "Updating...";
      person.FirstName = "Luke " + person.UpdateCounter;
      person = await person.SaveAsync();
      Status = "Updated";
    }
    catch (Exception ex)
    {
      Status = ex.ToString();
    }
  }
}
